package net.sf.midollo.net.httptunnel.server;

import java.io.IOException;
import java.io.InputStream;

public class HttpTunnelServerInputStream extends InputStream {

	protected final HttpTunnelServerSession session;
	protected final byte[] singleByteBuffer;

	public HttpTunnelServerInputStream(HttpTunnelServerSession session) throws IOException {
		this.session = session;
		this.singleByteBuffer = new byte[1];
	}

	public int read() throws IOException {
		int nread = read(this.singleByteBuffer, 0, 1);
		if (nread < 0)
			return -1;
		int c = this.singleByteBuffer[0];
		if (c < 0)
			c += 256;
		return c;
	}

	public int read(byte[] b) throws IOException {
		return read(b, 0, b.length);
	}

	public int read(byte[] b, int off, int len) throws IOException {
		return this.session.requestRead(b, off, len);
	}

	public long skip(long n) throws IOException {
		return super.skip(n);
	}

	public int available() throws IOException {
		return this.session.requestAvailable();
	}

	public void close() throws IOException {
	}

	public void mark(int readlimit) {
	}

	public void reset() throws IOException {
		throw new IOException("Unsupported operation.");
	}

	public boolean markSupported() {
		return false;
	}
}
